//
//  ReadTableViewCell.m
//  Cat
//
//  Created by dllo on 15/11/28.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ReadTableViewCell.h"

@implementation ReadTableViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.nameLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.nameLabel];
        
        self.timeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.timeLabel];
        
        self.headerImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:self.headerImageView];
        
        self.wordLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.wordLabel];
        
        self.commentsView = [[CYHHeaderView alloc] initWithFrame:CGRectMake(0, self.contentView.frame.size.height - 95, VIEW_WIDTH, 50)];
        [self.contentView addSubview:self.commentsView];
        
        self.lineView = [[UIView alloc] init];
        [self.contentView addSubview:self.lineView];
        
        self.likeLabel = [[UILabel alloc] init];
        [self.contentView addSubview:self.likeLabel];
        
        self.likeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.contentView addSubview:self.likeButton];
        [self.likeButton setImage:[UIImage imageNamed:@"iconfont-xihuan.png"] forState:UIControlStateNormal];
        
    }
    return self;
}

- (void)layoutSubviews {
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.headerImageView.layer.cornerRadius = 25;
//    self.headerImageView.backgroundColor = [UIColor redColor];
    self.headerImageView.layer.masksToBounds = YES;
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(5);
        make.top.equalTo(self.contentView.mas_top).with.offset(5);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(50);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).with.offset(10);
        make.left.equalTo(self.headerImageView.mas_right).with.offset(5);
        make.width.mas_equalTo(VIEW_WIDTH / 2);
        make.height.mas_equalTo(20);
    }];
//    self.nameLabel.backgroundColor = [UIColor yellowColor];
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nameLabel.mas_bottom).offset(0);
        make.left.equalTo(self.headerImageView.mas_right).offset(5);
        make.width.mas_equalTo(VIEW_WIDTH /2);
        make.height.mas_equalTo(15);
        
    }];
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    self.timeLabel.textColor = [UIColor grayColor];
    
    
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil];
    // 根据文本字体大小来计算文本的cell高度
    // 第二个参数:
    CGRect rect = [self.wordLabel.text boundingRectWithSize:CGSizeMake(VIEW_WIDTH, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 设置myLabel的尺寸
    self.wordLabel.frame = CGRectMake(10, 60, VIEW_WIDTH - 10, rect.size.height);
    self.wordLabel.font = [UIFont systemFontOfSize:15];
    self.wordLabel.numberOfLines = 0;
    [self.wordLabel sizeToFit];
    
    
    [self.commentsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lineView.mas_top).with.offset(-5);
        make.right.equalTo(self.contentView.mas_right).with.offset(0);
        make.height.mas_equalTo(50);
        make.left.equalTo(self.contentView.mas_left).with.offset(0);
    }];
//    self.commentsView.backgroundColor = [UIColor yellowColor];
    
    self.lineView.backgroundColor = [UIColor grayColor];
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.contentView);
        make.height.mas_equalTo(1);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-40);
    }];
    
    self.likeLabel.font = [UIFont systemFontOfSize:12];
    
    [self.likeButton setImage:[UIImage imageNamed:@"iconfont-xihuan.png"] forState:UIControlStateNormal];
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView.mas_right).with.offset(-10);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(30);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-5);
    }];
    
//    self.likeButton.backgroundColor = [UIColor redColor];
    [self.likeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.likeLabel.mas_left).with.offset(-10);
        make.width.mas_equalTo(30);
        make.height.mas_equalTo(30);
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-5);
    }];
}

- (void)setModel:(HotModel *)model {
    
    // 个人信息
    [ToolNetWorking ToolNetWorking:[NSString stringWithFormat:@"http://user.huapipi.com/userinfo/basic.do?uid=%@", model.uid] block:^(id result) {
        self.nameLabel.text = [NSString stringWithFormat:@"%@", result[@"nickname"]];
        [self.headerImageView sd_setImageWithURL:[NSURL URLWithString:result[@"portrait"]] placeholderImage:[UIImage imageNamed:@"ooopic_1445565008@2x.png"]];
    }];
    
}

@end
